using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Pigs.Enemies;
using Pigs.Weapons;

namespace pigs
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class DemoLevel : Level
    {
        Texture2D clouds;

        public DemoLevel(GameSettings set)
            :base(set)
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        /// <summary>
        /// LoadContent images for gameplay
        /// </summary>
        public override void LoadContent()
        {
            wave = new Boolean[10];
            this.loadCommonContent();

            //load mountain
            Texture2D mountainImage = content.Load<Texture2D>("levels/demo/background1");
            Scenery mountain = new Scenery(mountainImage, new Vector2(0, viewport.Height - mountainImage.Height));
            mountain.initialize(-.4f, false);
            collidableBackground.Add(mountain);
            mountainImage = content.Load<Texture2D>("levels/demo/background2");
            mountain = new Scenery(mountainImage, new Vector2((int)collidableBackground[collidableBackground.Count - 1].position.X + collidableBackground[collidableBackground.Count - 1].Width, viewport.Height - mountainImage.Height));
            mountain.initialize(-.4f, false);
            collidableBackground.Add(mountain);
            mountainImage = content.Load<Texture2D>("levels/demo/background3");
            mountain = new Scenery(mountainImage, new Vector2((int)collidableBackground[collidableBackground.Count - 1].position.X + collidableBackground[collidableBackground.Count - 1].Width, viewport.Height - mountainImage.Height));
            mountain.initialize(-.4f, false);
            collidableBackground.Add(mountain);
            mountainImage = content.Load<Texture2D>("levels/demo/background4");
            mountain = new Scenery(mountainImage, new Vector2((int)collidableBackground[collidableBackground.Count - 1].position.X + collidableBackground[collidableBackground.Count - 1].Width, viewport.Height - mountainImage.Height));
            mountain.initialize(-.4f, false);
            collidableBackground.Add(mountain);

            //load cloud
            clouds = content.Load<Texture2D>("levels/demo/cloudsflipped");
            Scenery cloud = new Scenery(clouds, new Vector2(0, 0));
            cloud.initialize(-0.1f, false);
            background.Add(cloud);
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            this.commonUpdate(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {

                //start wave 0 a single normal pig
                if (levelTime > 3000 && !wave[1])
                {
                    if (pigs.Count < 2)
                    {
                        addNinja(40, 600);
                        addLeader(40, 300);

                        //nPig = new Ninja_Pig(ninjaPig, new Vector2(viewport.Width - ninjaPig.Width, 600), this.numberOfPlayers);
                        //pigs.Add(nPig);

                        //dPig = new Drunk_Pig(drunkPig, new Vector2(viewport.Width - ninjaPig.Width, 200), this.numberOfPlayers);
                        //pigs.Add(dPig);

                        //sp = new SuicidePig(suiPig, new Vector2(viewport.Width - suiPig.Width, 400), this.numberOfPlayers);
                        //pigs.Add(sp);

                    }
                    //wave[1] = true;
                }

                ////start wave 2  - 4 suicide pigs
                //if (levelTime > 10000 && !wave[2])
                //{
                //    sp = new SuicidePig(this.suiPig, new Vector2(viewport.Width - suiPig.Width, 200));
                //    pigs.Add(sp);
                //    sp = new SuicidePig(this.suiPig, new Vector2(viewport.Width, 200));
                //    pigs.Add(sp);
                //    sp = new SuicidePig(this.suiPig, new Vector2(viewport.Width + suiPig.Width, 200));
                //    pigs.Add(sp);
                //    sp = new SuicidePig(this.suiPig, new Vector2(viewport.Width + 2*suiPig.Width, 200));
                //    pigs.Add(sp);
                //    wave[2] = true;
                //}

                ////wave 3 - steel pig
                //if (levelTime > 14000 && !wave[3])
                //{
                //    steelPig = new SteelPig(steelPigTexture, new Vector2(viewport.Width - basicPigTexture.Width / 5,
                //        player1.position.Y));
                //    this.steelPigPresent = true;
                //    pigs.Add(steelPig);
                //    wave[3] = true;
                //}

                ////wave 4 more normal pigs
                //if (levelTime > 18000 && !wave[4])
                //{
                //    for (int i = 0; i < 5; i++)
                //    {
                //        pig = new Basic_Pig(basicPigTexture, new Vector2(viewport.Width - basicPigTexture.Width / 5,
                //            rand.Next(SCORE_SPACE, viewport.Height - basicPigTexture.Height)));
                //        pigs.Add(pig);
                //    }
                //    wave[4] = true;
                //}

                ////wave 5 more normal pigs
                //if (levelTime > 29000 && !wave[5])
                //{
                //    for (int i = 0; i < 5; i++)
                //    {
                //        pig = new Basic_Pig(basicPigTexture, new Vector2(viewport.Width - basicPigTexture.Width / 5,
                //            rand.Next(SCORE_SPACE, viewport.Height - basicPigTexture.Height)));
                //        pigs.Add(pig);
                //    }
                //    wave[5] = true;
                //}

                ////wave 6 - suicide pig
                //if (levelTime > 31000 && !wave[6])
                //{
                //    sp = new SuicidePig(this.suiPig, new Vector2(viewport.Width - suiPig.Width, 200));
                //    pigs.Add(sp);
                //    wave[6] = true;
                //}

                ////wave 7 huge amount of pigs that repeats
                //if (levelTime > 41000 && !wave[7])
                //{
                //    if (pigs.Count < 150)
                //    {
                //        pig = new Basic_Pig(basicPigTexture, new Vector2(viewport.Width - basicPigTexture.Width / 5 + 1000,
                //            rand.Next(SCORE_SPACE, viewport.Height - basicPigTexture.Height)));
                //        pigs.Add(pig);
                //    }
                //}

                ////wave 8 - another suicide pig
                //if (levelTime > 90000 && !wave[8])
                //{
                //    sp = new SuicidePig(this.suiPig, new Vector2(viewport.Width - suiPig.Width, 200));
                //    pigs.Add(sp);
                //    wave[8] = true;
                //}

                //wave 9 load another mountain
                if (levelTime > 30000 && !wave[9])
                {
                    //create scenery items
                    Texture2D mountainImage = content.Load<Texture2D>("levels/demo/background1");
                    Scenery mountain = new Scenery(mountainImage, new Vector2(viewport.Width + 100, 0));
                    mountain.initialize(-.4f, true);
                    collidableBackground.Add(mountain);
                    mountainImage = content.Load<Texture2D>("levels/demo/background2");
                    mountain = new Scenery(mountainImage, new Vector2((int)collidableBackground[collidableBackground.Count - 1].position.X + collidableBackground[collidableBackground.Count - 1].Width, 0));
                    mountain.initialize(-.4f, true);
                    collidableBackground.Add(mountain);
                    mountainImage = content.Load<Texture2D>("levels/demo/background3");
                    mountain = new Scenery(mountainImage, new Vector2((int)collidableBackground[collidableBackground.Count - 1].position.X + collidableBackground[collidableBackground.Count - 1].Width, 0));
                    mountain.initialize(-.4f, true);
                    collidableBackground.Add(mountain);
                    mountainImage = content.Load<Texture2D>("levels/demo/background4");
                    mountain = new Scenery(mountainImage, new Vector2((int)collidableBackground[collidableBackground.Count - 1].position.X + collidableBackground[collidableBackground.Count - 1].Width, 0));
                    mountain.initialize(-.4f, true);
                    collidableBackground.Add(mountain);
                    wave[9] = true;
                }

                //tile the background clouds
                if (((int)background[background.Count - 1].position.X + background[background.Count - 1].Width) < (viewport.Width + 100))
                {
                    Scenery cloud = new Scenery(clouds, new Vector2((int)background[background.Count - 1].position.X + background[background.Count - 1].Width - 1, 0));
                    cloud.initialize(-0.1f, false);
                    background.Add(cloud);
                }
            }
        }
        
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            this.commonDraw(gameTime);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
